<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends BaseController {
    //添加代理
    public function adminAdd(){
        $this->display();
    }
    //添加代理数据保存
    public function adminSave(){
        $data=I('post.');
        $aOb=M('Admin');
        //判断身份证号是否已存在
        if ($data['id_no']) {
            $id_exist=$aOb->field('admin_id')->where('id_no="'.$data['id_no'].'"')->find();
            if ($id_exist) {
                $this->ajaxReturn('id_exist');
            }
        }
        //判断推荐码是否已存在
        if ($data['p_code']) {
            $pid=$aOb->field('admin_id,node_id')->where('code='.$data['p_code'])->find();
            if ($pid) {
                $data['pid']=$pid['admin_id'];
                $data['node_id']=$pid['node_id'].$pid['admin_id'].'|';
            }else{
                $this->ajaxReturn('code_nexist');
            }
        }
        //判断手机号是否已存在
        if ($data['phone']) {
            $phone_exist=$aOb->field('admin_id')->where('phone="'.$data['phone'].'"')->find();
            if ($phone_exist) {
                $this->ajaxReturn('phone_exist');
            }
        }
        
        if ($data['role']==1) {
            $last_code=$aOb->field('code')->where('role=1')->order('admin_id desc')->find();
            if ($last_code) {
                $data['code']=$last_code['code']+1;
            }else{
                $data['code']=10001;
            }
        }
        
        $data['addtime']=time();
        $data['password']=md5($data['password']);
        $re=$aOb->add($data);
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
    
    //购买审核列表
    public function buyReview(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        if ($keywords!='' && $keyvalue!='') {
            if ($keywords=='truename') {
               $where='where a.'.$keywords.' like "%'.$keyvalue.'%" ';
            }else{
                $where='where b.'.$keywords.' like "%'.$keyvalue.'%" ';
            }
        }else if ($keywords!='' && $keyvalue=='') {
            $where='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where='where b.status like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%"';
        }
        $re=M('')->query('select b.*,FROM_UNIXTIME(b.addtime, "%Y-%m-%d %H:%i:%S") as addtime,FROM_UNIXTIME(b.modify_time, "%Y-%m-%d %H:%i:%S") as modify_time,a.truename,a.phone from gm_buy b join gm_admin a on a.admin_id=b.admin_id '.$where.'order by b.status asc,b.id desc');
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }
    //提现审核列表
    public function withdrawReview(){
        $keywords=I('get.keywords');
        $keyvalue=trim(I('get.keyvalue'));
        if ($keywords!='' && $keyvalue!='') {
            if ($keywords=='truename') {
               $where='where a.'.$keywords.' like "%'.$keyvalue.'%" ';
            }else{
                $where='where r.'.$keywords.' like "%'.$keyvalue.'%" ';
            }
        }else if ($keywords!='' && $keyvalue=='') {
            $where='';
        }else if ($keywords=='' && $keyvalue!='') {
            $where='where r.status like "%'.$keyvalue.'%" or a.truename like "%'.$keyvalue.'%" ';
        }
        $re=M('')->query('select r.*,FROM_UNIXTIME(r.addtime, "%Y-%m-%d %H:%i:%S") as addtime,a.truename,a.phone from gm_withdraw r join gm_admin a on a.admin_id=r.admin_id '.$where.'order by r.status asc,r.id desc');
        // var_dump($re);die;
        if (!$re) {
            $re='';
        }
        $data=$this->showPage($re);
        
        $this->assign('adminList',$data['arr']);
        $this->assign('page',$data['pageStr']);
        $this->assign('keywords',$keywords);
        $this->assign('keyvalue',$keyvalue);
        $this->display();
    }
    //数据统计列表
    public function totalList(){
        $gamer_sum=M('Gamer')->count();
        $admin_sum=M('Admin')->where('role=1')->count();
        $buy_sum=M('Admin')->where('role=1')->sum('total_pay');
        $pay_sum=M('Gamer')->sum('total_pay');
        $consume_sum=M('Gamer')->sum('total_consume');
        $income_sum=M('Admin')->where('role=1')->sum('total_amount');
        $rebate_sum=M('Admin')->where('role=1')->sum('total_rebate');
        $withdraw_sum=M('Admin')->where('role=1')->sum('total_withdraw');
        $this->assign('gamer_sum',$gamer_sum);
        $this->assign('admin_sum',$admin_sum);
        $this->assign('buy_sum',$buy_sum);
        $this->assign('pay_sum',$pay_sum);
        $this->assign('consume_sum',$consume_sum);
        $this->assign('income_sum',$income_sum);
        $this->assign('rebate_sum',$rebate_sum);
        $this->assign('withdraw_sum',$withdraw_sum);
        $this->display();
    }
    
    //返利规则
    public function rebateRule(){
        $re=M('Rebate_rule')->find();
        if ($re) {
            $this->assign('re',$re);
        }
        $this->display();
    }
    //返利规则数据保存
    public function ruleSave(){
        $data=I('post.');
        $id=I('get.id');
        if ($id) {
            $re=M('Rebate_rule')->where('id='.$id)->save($data);
        }else{
            $re=M('Rebate_rule')->add($data);
        }
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
    //购买/提现审核改状态
    public function review(){
        $id=I('get.id');
        $action=I('get.action');
        $data['status']=I('get.status');
        $data['note']=I('post.note');
        $data['amount']=I('post.amount');
        $num=I('post.num');
        $admin_id=I('post.admin_id');
        $withdrawAmount=I('post.withdrawAmount');
        $data['modify_time']=time();
        $re=M($action)->where('id='.$id)->save($data);
        $mOb=M('Admin');
        if ($re) {
            if ($action=='Buy' && $data['status']==1) {

                //代理购买数量累加
                $bre=$mOb->where('admin_id='.$admin_id)->setInc('total_pay',$num);
                if ($bre) {
                    //代理消费总额累加
                    $mOb->where('admin_id='.$admin_id)->setInc('total_amount',$data['amount']);
                }
                //获取上一级id
                $f_pid=$mOb->field('pid')->where('admin_id='.$admin_id)->find();
                if ($f_pid['pid']!=0) {
                    $rebateRule=M('Rebate_rule')->find();
                    if ($rebateRule['f_scale'] != 0) {
                        //上一级返利
                        $arr1['admin_id']=$f_pid['pid'];
                        $arr1['buyer_id']=$admin_id;
                        $arr1['amount']=$data['amount'];
                        $arr1['rebate_scale']=round($rebateRule['f_scale']/100,2);
                        $arr1['rebate']=round($arr1['rebate_scale']*$arr1['amount'],2);
                        $arr1['addtime']=time();
                        $rre1=M('Rebate')->add($arr1);
                        if ($rre1) {
                            //一级代理返利总额累加
                            $mOb->where('admin_id='.$arr1['admin_id'])->setInc('total_rebate',$arr1['rebate']);
                            if ($rebateRule['s_scale'] != 0) {
                                //获取上二级id
                                $s_pid=$mOb->field('pid')->where('admin_id='.$arr1['admin_id'])->find();
                                if ($s_pid['pid']!=0) {
                                    //上二级返利
                                    $arr2['admin_id']=$s_pid['pid'];
                                    $arr2['buyer_id']=$admin_id;
                                    $arr2['amount']=$data['amount'];
                                    $arr2['rebate_scale']=round($rebateRule['s_scale']/100,2);
                                    $arr2['rebate']=round($arr2['rebate_scale']*$arr2['amount'],2);
                                    $arr2['addtime']=time();
                                    $rre2=M('Rebate')->add($arr2);
                                    if ($rre2) {
                                        //二级代理返利总额累加
                                        $mOb->where('admin_id='.$arr2['admin_id'])->setInc('total_rebate',$arr2['rebate']);
                                        if ($rebateRule['t_scale'] != 0) {
                                            //获取上三级id
                                            $t_pid=$mOb->field('pid')->where('admin_id='.$arr2['admin_id'])->find();
                                            if ($t_pid['pid']!=0) {
                                                //上三级返利
                                                $arr3['admin_id']=$t_pid['pid'];
                                                $arr3['buyer_id']=$admin_id;
                                                $arr3['amount']=$data['amount'];
                                                $arr3['rebate_scale']=round($rebateRule['t_scale']/100,2);
                                                $arr3['rebate']=round($arr3['rebate_scale']*$arr3['amount'],2);
                                                $arr3['addtime']=time();
                                                $rre3=M('Rebate')->add($arr3);
                                                if ($rre3) {
                                                    //三级代理返利总额累加
                                                    $mOb->where('admin_id='.$arr3['admin_id'])->setInc('total_rebate',$arr3['rebate']);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }                
            }
            if ($action=='Withdraw' && $data['status']==1) {
                //代理提现总额累加
                $mOb->where('admin_id='.$admin_id)->setInc('total_withdraw',$withdrawAmount);
            }
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }

    //购买/提现记录删除
    public function del(){
        $id=I('get.id');
        $action=I('get.action');
        $re=M($action)->where('id='.$id)->delete();
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('false');
        }
    }
}